import request from '@/utils/request'

const modelApi = {
  ListModels: '/mlflow/registered-models/search',
  AddModels: '/flask/createModel',
  DeleteModel: '/mlflow/registered-models/delete',
  ModelDetail: '/mlflow/registered-models/get',
  SetTags: '/mlflow/registered-models/set-tag',
  CreateTask: '/flask/createTask',
  ListTask: '/flask/tasks',
  RunTask: '/flask/runTask',
  GetTaskResult: '/flask/taskResult',
  DeleteTask: '/flask/deleteTask',
  SetDesc: '/mlflow/registered-models/update',
}

// mlflow的查询参数要求为 filter=name ilike 'test'
export function listModels(parameters) {
  return request({
    url:
      parameters.name != null && parameters.name !== ''
        ? `${modelApi.ListModels}?filter=${encodeURIComponent("name ilike '%" + parameters.name + "%'")}`
        : modelApi.ListModels,
    method: 'get',
    param: parameters,
  })
}

export function addModel(parameters) {
  return request({
    url: modelApi.AddModels,
    method: 'post',
    data: parameters,
  })
}

// 删除使用名字
export function deleteModel(parameters) {
  return request({
    url: modelApi.DeleteModel,
    method: 'delete',
    data: parameters,
  })
}

export function getModelDetail(parameters) {
  return request({
    url: `${modelApi.ModelDetail}?name=${parameters.name}`,
    method: 'get',
    data: parameters,
  })
}

export function setDesc(parameters) {
  return request({
    url: `${modelApi.SetDesc}`,
    method: 'patch',
    data: parameters,
  })
}

export function setTags(parameters) {
  return request({
    url: `${modelApi.SetTags}`,
    method: 'post',
    data: parameters,
  })
}

export function createTask(parameters) {
  return request({
    url: `${modelApi.CreateTask}`,
    method: 'post',
    data: parameters,
  })
}

export function listTask(parameters) {
  return request({
    url: `${modelApi.ListTask}`,
    method: 'get',
    data: parameters,
  })
}

export function runTask(parameters) {
  return request({
    url: `${modelApi.RunTask}`,
    method: 'post',
    data: parameters,
  })
}

export function getTaskResult(parameters) {
  return request({
    url: `${modelApi.GetTaskResult}?lastRunResult=${parameters.lastRunResult}`,
    method: 'get',
    data: parameters,
  })
}

export function deleteTask(parameters) {
  return request({
    url: `${modelApi.DeleteTask}?taskName=${parameters.taskName}`,
    method: 'DELETE',
    data: parameters,
  })
}
